<template>
  <el-form ref="userRef" :model="form" :rules="rules" label-width="80px">
    <el-form-item label="城市" >
      <el-input v-model="form.stuCity" maxlength="30" />
    </el-form-item>
    <el-form-item label="户籍地" >
      <el-input v-model="form.stuPlace" maxlength="30" />
    </el-form-item>
    <el-form-item label="手机号码" >
      <el-input v-model="form.stuPhone" maxlength="11" />
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="form.sex">
        <el-radio value="0">男</el-radio>
        <el-radio value="1">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="学院" >
      <el-select v-model="form.collegeId" placeholder="请选择学院" style="width: 200px" @change="changeCollege">
        <el-option v-for="item in state.collegeList" :key="item.id" :label="item.collegeName"
                   :value="item.id"/>
      </el-select>
    </el-form-item>
    <el-form-item label="系" >
      <el-select v-model="form.facultiesId" placeholder="请选择系" style="width: 200px" @change="changeFaculties">
        <el-option v-for="item in state.filterFacultiesList" :key="item.id" :label="item.facultiesName"
                   :value="item.id"/>
      </el-select>
    </el-form-item>
    <el-form-item label="班级" >
      <el-select v-model="form.classId" placeholder="请选择系" style="width: 200px">
        <el-option v-for="item in state.filterClassList" :key="item.id" :label="item.className"
                   :value="item.id"/>
      </el-select>
    </el-form-item>
    <el-form-item label="专业" >
        <el-input v-model="form.major"   />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { updateUserProfile } from "@/api/system/user";
import {getAllClassListApi, getAllFacultiesApi, getCollegeAllListApi} from "@/api/school.js";

const props = defineProps({
  user: {
    type: Object
  }
});

const { proxy } = getCurrentInstance();
const emit = defineEmits(["changeInfo"]);
const form = ref({});
const rules = ref({
  nickName: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
  email: [{ required: true, message: "邮箱地址不能为空", trigger: "blur" }, { type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }],
  phonenumber: [{ required: true, message: "手机号码不能为空", trigger: "blur" }, { pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }],
});

const state = reactive({
  collegeList: [],
  facultiesList: [],
  filterFacultiesList: [],
  classList: [],
  filterClassList: [],
})

const getCollegeList = async () => {
  const res = await getCollegeAllListApi()
  if (res.code == 200) {
    state.collegeList = res.data
  }
}

const getAllFaculties = async () => {
  const res = await getAllFacultiesApi()
  if (res.code == 200) {
    state.facultiesList = res.data
    state.filterFacultiesList = res.data
  }
}

const getCLassListAll = async () => {
  const res = await getAllClassListApi()
  if (res.code == 200) {
    state.classList = res.data
    state.filterClassList = res.data
  }
}

/** 提交按钮 */
function submit() {
  proxy.$refs.userRef.validate(valid => {
    if (valid) {
      updateUserProfile(form.value).then(response => {
        proxy.$modal.msgSuccess("修改成功");
        props.user.phonenumber = form.value.phonenumber;
        props.user.email = form.value.email;
        emit("changeInfo");
      });
    }
  });
};

/** 关闭按钮 */
function close() {
  proxy.$tab.closePage();
};

const changeCollege = (id) => {
  state.filterFacultiesList = state.facultiesList.filter(item => item.collegeId === id)
}

const changeFaculties = (id) => {
  state.filterClassList = state.classList.filter(item => item.facultiesId === id)
}



onMounted(() => {
  getCollegeList()
  getAllFaculties()
  getCLassListAll()


  // 回显当前登录用户信息
  watch(() => props.user, user => {
    if (user) {
      console.log(props.user,":props.user")
      form.value = JSON.parse(JSON.stringify(props.user));
    }
  },{ immediate: true });
})
</script>
